﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Diagnostics;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.ServiceModel;

using QCM.Framework;
using QCM.Forms.Properties;

namespace QCM.Forms
{
    public partial class Form1 : Form
    {
        ServiceReference1.ContractClient proxy;
        Font fontTitleQuestion = null;

        public Form1()
        {
            Trace.WriteLine("Démarrage de Form1","Catégorie");
            this.fontTitleQuestion = Settings.Default.FontTitleQuestion;
            InitializeComponent();
            splitContainer1.Panel2.Enabled = false;
        }

        private void RefreshTreeView_Click(object sender, EventArgs e)
        {
            this.treeView1.Nodes.Clear();
            ServiceReference1.Categorie[] cats;

            //Recupere la liste des categoories en interrogeant le service
            try
            {
                cats = proxy.GetCategories();
            }
            catch (FaultException<ServiceReference1.SQLFaultException> exp)
            {
                MessageBox.Show("Reason:" + exp.Reason + "Code: " + exp.Code.Name, "SQLFaultException");
                cats = null;
            }
            catch (FaultException exp)
            {
                MessageBox.Show("Reason:" + exp.Reason + "Code: " + exp.Code.Name, "FaultException");
                cats = null;
            }
            catch (Exception exp)
            {
                MessageBox.Show(exp.GetType().ToString());
                cats = null;
            }
            finally
            {
                this.toolStripStatusLabel2.Text = proxy.State.ToString();
            }
            //Ajoute des categories dans le treeView
            if (cats != null)
            {
                foreach (ServiceReference1.Categorie cat in cats)
                {
                    TreeNode tn = new TreeNode(cat.Nom) { Name = cat.Nom };

                    if (cat.NomMere == string.Empty)
                    {
                        this.treeView1.Nodes.Add(tn);
                    }
                    else
                    {
                        TreeNode[] treeNodes = treeView1.Nodes.Find(cat.NomMere, true);
                        if (treeNodes.Length == 1)
                        {
                            treeNodes[0].Nodes.Add(tn);
                        }
                        else
                        {
                            throw new Exception("Problème de catégorie: " + cat.NomMere + ", cette categorie n'existe pas ou est en doublon");
                        }

                    }
                    //On ajoute a la nouvelle categorie les questionnaires associés
                    foreach (string titre in proxy.GetQuestionnaires(tn.Text))
                    {
                        TreeNode tnQuest = new TreeNode(titre);
                        tnQuest.Tag = "Questionnaire";
                        tn.Nodes.Add(tnQuest);
                    }
                }
            }
        }
        ServiceReference1.Questionnaire selectedQuestionnaire;
        private void treeView1_BeforeSelect(object sender, TreeViewCancelEventArgs e)
        {
            if (e.Node.Tag != null)
            {
                selectedQuestionnaire = proxy.GetQuestionnaire(e.Node.Text);
                splitContainer1.Panel2.Enabled = true;
                this.textBox1.Text = selectedQuestionnaire.Descriptif;
                this.label4.Text = selectedQuestionnaire.Questions.Length.ToString();
                selectedQuestionnaire.Questions.Reverse();

                this.panel1.Controls.Clear();
                foreach (ServiceReference1.Question quest in selectedQuestionnaire.Questions)
                {
                    QuestionCtrl ctrl = new QuestionCtrl();
                    this.panel1.Controls.Add(ctrl);

                    ctrl.Dock = System.Windows.Forms.DockStyle.Top;
                    ctrl.FontTitle = fontTitleQuestion;
                    ctrl.Name = quest.Titre;
                    ctrl.LaQuestion = null;
                    //                    ctrl.Size = new System.Drawing.Size(464, 150);
                }
            }

            else
            {
                splitContainer1.Panel2.Enabled = false;
                selectedQuestionnaire = null;
                this.textBox1.Text = string.Empty;
                this.label4.Text = "0";
                this.panel1.Controls.Clear();
            }
        }

        private void chargerUneFontToolStripMenuItem_Click(object sender, EventArgs e)
        {
            FontDialog fd = new FontDialog();
            if (fd.ShowDialog() == DialogResult.OK)
            {
                fontTitleQuestion = fd.Font;
                foreach (Object qctrl in panel1.Controls)
                {
                    if(qctrl.GetType() == typeof(QuestionCtrl))
                        ((QuestionCtrl)qctrl).FontTitle = fontTitleQuestion;
                }
            }
        }

        private void seConnecterToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.proxy = new QCM.Forms.ServiceReference1.ContractClient();
            this.toolStripStatusLabel2.Text = this.proxy.State.ToString();
        }
    }
}
